﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using Tracy.CoreLib.Data;

namespace Tracy.CoreLib
{
    public class FilterEngine
    {
        public static IXmlModelCollection ApplySchema(RssFeedSource source, IManagedXmlModelCollection siteSchemas)
        {
            return ApplySchema(source, (SiteSchema)siteSchemas.GetItemById(source.SchemaId));
        }

        public static IXmlModelCollection ApplySchema(RssFeedSource source, SiteSchema siteSchema)
        {
            return ApplySchema(source.OriginalRss, siteSchema);
        }

        public static IXmlModelCollection ApplySchema(RssModel rss, SiteSchema siteSchema)
        {
            ResourceItem newItem = null;
            IXmlModelCollection output = new XmlModelCollection();
            string tmpLink;
            foreach (RssModel.RssItem rssItem in rss.Channel.Items)
            {
                tmpLink = siteSchema.Filter.Match(rssItem.GetValue(siteSchema.Target));
                if (tmpLink != null && tmpLink != "")
                {
                    newItem = new ResourceItem();
                    newItem.Title = rssItem.Title;
                    newItem.Link = tmpLink;
                    newItem.Status = Constants.CONST_RESOURCE_STATUS_RAW;
                    newItem.Type = siteSchema.Type;
                    newItem.LastUpdate = System.DateTime.Now;
                    output.Add(newItem);
                }
            }
            return output;
        }

        /// <summary>
        /// 从RssFeedSource中过滤出匹配的Resource
        /// </summary>
        /// <param name="sources">Rss源</param>
        /// <param name="rssFilter">过滤器</param>
        /// <param name="siteSchemas">站点模式</param>
        public static IXmlModelCollection MatchRssItems(IManagedXmlModelCollection sources, RssFilter rssFilter, IManagedXmlModelCollection siteSchemas)
        {
            IXmlModelCollection newResources = new XmlModelCollection();
            IXmlModelCollection tmpResults = null;
            ResourceItem newItem = null;

            foreach (RssFeedSource source in sources)
            {
                tmpResults = ApplySchema(source, siteSchemas);
                foreach (ResourceItem item in tmpResults)
                {
                    newItem = MatchResourceItem(item, rssFilter);
                    if (newItem != null)
                    {
                        newResources.Add(newItem);
                    }
                }
            }
            return newResources;
        }

        public static ResourceItem MatchResourceItem(ResourceItem item, RssFilter rssFilter)
        {
            //ResourceItem newResource = null;
            //string link = null;
            ////TODO: 目前只匹配Title，将来能够匹配各种属性
            //string matchResult = rssFilter.Filter.Match(item.Title);
            //if (matchResult != null && matchResult != "")
            //{
            //        newResource = new ResourceItem();//TODO: ResourceItem应该由Provider创建
            //        newResource.Link = item.Link;
            //        newResource.Title = item.Title;
            //        newResource.Description = item.Description;
            //        newResource.LastUpdate = item.LastUpdate; //TODO: 不太准确，将来考虑用Provider的更新时间
            //        newResource.Status = Constants.CONST_RESOURCE_STATUS_NEW; //TODO: 暂时用string，考虑到多语言界面，改用int序号比较合适
            //        newResource.Folder = rssFilter.Folder;
            //    //TODO: RssModel应该有对RssFeedSource的引用，以获得newResource.Type
            //    //TODO: filename是否需要有待考察
            //}
            //return newResource;

            //TODO: 目前只匹配Title，将来能够匹配各种属性
            string matchResult = rssFilter.Filter.Match(item.Title);
            if (matchResult != null && matchResult != "")
            {
                item.Status = Constants.CONST_RESOURCE_STATUS_NEW; //TODO: 暂时用string，考虑到多语言界面，改用int序号比较合适
                item.Folder = rssFilter.Folder;
                return item;
                //TODO: filename是否需要有待考察
            }
            else
            {
                return null;
            }
            
        }

        //public static XmlModelCollection MatchRss
    }
}
